package com.xinnet.mms.deployapproval.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xinnet.mms.allot.service.CustomAllotService;
import com.xinnet.mms.customerManage.dao.CustomerManageDao;
import com.xinnet.mms.customerstate.dao.CustomerStateHistDao;
import com.xinnet.mms.deployapproval.service.CustomDeployApprovalService;
import com.xinnet.mms.empcustomrelation.dao.EmpCustomRelationDao;
import com.xinnet.mms.entity.CustomMixAudit;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.utils.CustomerManage;

@Service("customDeployApprovalService")
public class CustomDeployApprovalServiceImpl implements CustomDeployApprovalService{
	
	@Autowired
	private EmpCustomRelationDao empCustomRelationDao;
	@Autowired
	private CustomerStateHistDao customerStateHistDao;
	@Autowired
	private CustomAllotService customAllotService;
	@Autowired
	private CustomerManageDao customerManageDao;
	
	private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
	Logger log=LoggerFactory.getLogger(CustomDeployApprovalServiceImpl.class);
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public int approval(String customMixId,String isEffect,String approvalerId,String deptIdStr,String productId,String productCode) throws Exception{
	//	WriteLock writeLock = readWriteLock.writeLock();
		try {
			//writeLock.lock();
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("id", customMixId);
			param.put("deptId", deptIdStr);
			param.put("approvalerId", approvalerId);
			List<CustomMixAudit> customMixAuditList = customerManageDao.getCustomMixAudit(param);
			if(customMixAuditList!=null && customMixAuditList.size()>0){
				CustomMixAudit customMixAudit = customMixAuditList.get(0);
				String cusId = customMixAudit.getCusId();
				String isEffectFromDB = customMixAudit.getIsEffect();
				if("2".equals(isEffectFromDB)){
					if("1".equals(isEffect)){
						String operationContent = customMixAudit.getOperationContent();
						if (operationContent != null && !"".equals(operationContent)) {
							String deptIdAndEmpNo = operationContent.split(",")[1];
							String currentDeptId = deptIdAndEmpNo.split("_")[0];
							String currentEmpNo = deptIdAndEmpNo.split("_")[1];
							String deployDeptId = deptIdAndEmpNo.split("_")[2];
							String deployEmpNo = deptIdAndEmpNo.split("_")[3];
							if("*".equals(deployEmpNo)){
								deployEmpNo=null;
							}

							Customer customer = customerManageDao.getDeployCustomByCustomMixId(customMixId);
							customer.setMixDeptno(deployDeptId);
							customer.setMixEmpno(deployEmpNo);
							customer.setEmpId(customMixAudit.getFromEmpId());
							customer.setPusherId(customMixAudit.getApplyerId());
							customer.setCustomState(CustomerManage.ACCEPT_YES);
							customer.setProductCode(productCode);
							/*customerStateHistDao.updateCustomStateHistByCustomIdAndEmpNo(cusId,currentEmpNo,productId);*/
							customAllotService.allot(customer, CustomerManage.Mix_CUSTOMER);
							
							customerManageDao.updateCustomMixAuditFlagByCustomMixId(customMixId);
						}
					}
					CustomMixAudit customMixAuditParam = new CustomMixAudit();
					customMixAuditParam.setIsEffect(isEffect);
					customMixAuditParam.setId(customMixId);
					customMixAuditParam.setApprovalerId(approvalerId);
					customerManageDao.updateCustomMixAudit(customMixAuditParam);
					return 1;
				}
			}else{
				return 2;
			}

		} catch (Exception e) {
			log.error("调配审核异常",e);
			e.printStackTrace();
			return 3;
		}finally{
			//writeLock.unlock();
		}
		return 0;
	}
	
	public int bathApproval(String customMixIdStr,String isEffect,String approvalerId,String deptId) throws Exception{
		//WriteLock writeLock = readWriteLock.writeLock();
		int result = 1;
		try {
			//writeLock.lock();
			String[]  customMixIdArray = customMixIdStr.split(",");
			for (int i = 0; i < customMixIdArray.length; i++) {
				String customMixId = customMixIdArray[i];
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", customMixId);
				param.put("deptId", deptId);
				param.put("approvalerId", approvalerId);
				List<CustomMixAudit> customMixAuditList = customerManageDao.getCustomMixAudit(param);
				if(customMixAuditList!=null && customMixAuditList.size()>0){
					CustomMixAudit customMixAudit = customMixAuditList.get(0);
					String isEffectFromDB = customMixAudit.getIsEffect();
					String productCode = customMixAudit.getProductCode();
					String productId = customMixAudit.getProductId();
					if("2".equals(isEffectFromDB)){
						String cusId = customMixAudit.getCusId();
						int version = customMixAudit.getVersion();
						if("1".equals(isEffect)){
							String operationContent = customMixAudit.getOperationContent();
							if (operationContent != null && !"".equals(operationContent)) {
								String deptIdAndEmpNo = operationContent.split(",")[1];
								String currentDeptId = deptIdAndEmpNo.split("_")[0];
								String currentEmpNo = deptIdAndEmpNo.split("_")[1];
								String deployDeptId = deptIdAndEmpNo.split("_")[2];
								String deployEmpNo = deptIdAndEmpNo.split("_")[3];
								if("*".equals(deployEmpNo)){
									deployEmpNo=null;
								}
								
								Customer customer = customerManageDao.getDeployCustomByCustomMixId(customMixId);
								customer.setMixDeptno(deployDeptId);
								customer.setMixEmpno(deployEmpNo);
								empCustomRelationDao.delEmpCustomRelationByCustomIdAndEmpNo(cusId,currentEmpNo,productCode);
								customerStateHistDao.updateCustomStateHistByCustomIdAndEmpNo(cusId,currentEmpNo,productId);
								customer.setCustomState(CustomerManage.ACCEPT_YES);
								customAllotService.allot(customer, CustomerManage.Mix_CUSTOMER);
								customerManageDao.updateCustomMixAuditFlagByCustomMixId(customMixId);
							}
						}
						CustomMixAudit customMixAuditParam = new CustomMixAudit();
						customMixAuditParam.setIsEffect(isEffect);
						customMixAuditParam.setId(customMixId);
						customMixAuditParam.setApprovalerId(approvalerId);
						customMixAuditParam.setVersion(version);//数据库没有的字段
						customerManageDao.updateCustomMixAudit(customMixAuditParam);
						result = 1;
					}else{
						result=0;
					}
				}else{
					result=2;
				}
			}	
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 3;
		}finally{
			//writeLock.unlock();
		}
	}
}
